<?php
/**
 * +----------------------------------------------------------------------
 * 部门管理
 * +----------------------------------------------------------------------
 * Author: ZF <ZFajpst_2008@126.com> 2012-10-23 10:27:00
 * +----------------------------------------------------------------------
 */
 
	class Depart_model extends CI_Model {
	
		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
	
		/**
		 * +---------------------
		 * 部门管理
		 * +---------------------
		 */
		public function index(){
			return $this->getDepart();
		}
	
		/**
		 * +---------------------
		 * 部门添加
		 * +---------------------
		 */
		public function add(){
			$data = array(
				'pid' => $this->input->post('pid'),
				'name' => $this->input->post('name'),
				'notes' => $this->input->post('notes'),
				'ordernum' => $this->input->post('ordernum'),
				'addtime' => time()
			);
			
			return $this->db->insert('oa_depart', $data);
		}
		
		/**
		 * +---------------------
		 * 部门修改
		 * +---------------------
		 */
		public function edit($id, $action = ''){
			// 显示
			if($action != 'do'){
				$query = $this->db->get_where('oa_depart', array('id' => $id));
				return $query->row_array();
			}
			
			// 执行
			$data = array(
				'pid' => $this->input->post('pid'),
				'name' => $this->input->post('name'),
				'notes' => $this->input->post('notes'),
				'ordernum' => $this->input->post('ordernum')
			);
			
			return $this->db->update('oa_depart', $data, array('id' => $id));
		}
		
		/**
		 * +---------------------
		 * 部门删除
		 * +---------------------
		 */
		public function del($id){
			$this->db->select('id');
			$query = $this->db->get_where('oa_depart', array('pid' => $id));
			
			$rs = $query->result_array();
			
			if($rs){
				foreach($rs as $v){
					$this->del($v['id']);
				}
			}
			
			$this->db->delete('oa_depart', array('id' => $id));
			
			return true;
		}
		
		/**
		 * +---------------------
		 * 分配权限
		 * +---------------------
		 */
		public function power($id, $action = ''){
			// 显示
			if($action != 'do'){
				// 职位信息
				$this->db->select('name, notes');
				$query = $this->db->get_where('oa_depart', array('id' => $id));
				$data[0] = $query->row_array();
				
				// 获取职位对应权限
				$this->db->select('nodeid');
				$query = $this->db->get_where('oa_departnode', array('departid' => $id));
				$pArr = $query->result_array();
				
				if(empty($pArr)){
					$data[99][] = -1;			// 保证无权限(或第一次添加权限)用户获取正常
				}else{
					foreach($pArr as $hp){
						$data[99][] = $hp['nodeid'];
					}
				}
				
				// 权限查询
				$orderStr = 'id asc';
			
				// 部门管理
				$this->db->order_by($orderStr);		
				$query = $this->db->get_where('oa_node', array('pid' => 1, 'display' => 1));
				$data[1] = $query->result_array();
				
				// 用户管理
				$this->db->order_by($orderStr);
				$query = $this->db->get_where('oa_node', array('pid' => 7, 'display' => 1));
				$data[2] = $query->result_array();
				
				// 公文管理
				$this->db->order_by($orderStr);
				$query = $this->db->get_where('oa_node', array('pid' => 12, 'display' => 1));
				$data[3] = $query->result_array();
				
				return $data;
			}
			
			// 执行
			$this->db->delete('oa_departnode', array('departid' => $id));
			
			// 分配权限
			if($this->input->post('nodeid')){
				foreach($this->input->post('nodeid') as $nodeid){
					$data = array(
						'departid' => $id,
						'nodeid' => $nodeid
					);
					$this->db->insert('oa_departnode', $data);
				}
			}
			
			return true;
		}
		
		/**
		 * +---------------------
		 * 获取全部部门
		 * +---------------------
		 */
		public function getDepart(){
			// 获取全部数据
			$this->db->order_by('ordernum asc, id asc');
			$query = $this->db->get('oa_depart');
			$departArr = $query->result_array();
			
			// 按PID分组存放数据
			$tree = array();
			foreach($departArr as $v){
				$list = @$tree[$v['pid']] ? $tree[$v['pid']] : array();
				array_push($list, $v);
				$tree[$v['pid']] = $list;
			}
			
			return $tree;
		}		
		
		/**
		 * +---------------------
		 * 递归查询数组
		 * +---------------------
		 */
		public $datas = array();
		public function drawTree($arr, $tree, $level, $str = '|—'){
			// 等级
			$level++;
			
			// 等级前缀
			$prefix = '';
			for($i = 0; $i < $level; $i++){
				$prefix .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$prefix .= $str;
			
			// 递归查询信息并返回
			$narr = array();
			foreach($arr as $v){
				$narr['name'] = $prefix . $v['name'];
				$narr['id'] = $v['id'];
				array_push($this->datas, $narr);
				if(isset($tree[$v['id']])){
					$str == '|—' ? $this->drawTree($tree[$v['id']], $tree, $level) : $this->drawTree($tree[$v['id']], $tree, $level, $str);
				}
			}
			return $this->datas;
		}
	
	}